//
//  LLPublish2ViewController.m
//  LoveLove
//
//  Created by 达迩科技 on 2019/4/10.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLPublish2ViewController.h"
#import "LLPublish2TableViewCell.h"
#import "LLSearchView.h"
#import "AppDelegate.h"

@interface LLPublish2ViewController ()<UINavigationControllerDelegate,UITextFieldDelegate,UIApplicationDelegate>
@property (copy, nonatomic) NSArray *titlesArray; //标题数组
@property (copy, nonatomic) NSArray *titlesArray1;//图片数组
@property (strong, nonatomic) LLSearchView *searchView;//图片数组
@property (nonatomic,strong) NSIndexPath *lastIndex;
@property (nonatomic,assign) NSInteger selectedIndex;
@property (nonatomic,assign) BOOL isSecond;
@end

@implementation LLPublish2ViewController

- (void)viewWillAppear:(BOOL)animated {
    [[IQKeyboardManager sharedManager] setEnable:NO];
    [super viewWillAppear:animated];
    [SVProgressHUD dismiss];
    self.navigationController.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //TODO: 页面Disappear 启用
    [[IQKeyboardManager sharedManager] setEnable:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleView];
    self.titleLabel.text = @"发 布";
    [self.leftButton setTitle:@"上一步" forState:0];
    [self.leftButton setImage:nil forState:0];
    self.leftButton.frame = CGRectMake(20, self.navH-33, 80, 18);
    [self.leftButton setTitleColor:LLColorRGB(0) forState:0];
    self.leftButton.titleLabel.font = LLRegularFont(17);
    [self.rightButton setTitle:@"确定" forState:0];
    [self.rightButton setTitleColor:LLColor(249, 1, 0) forState:0];
    self.rightButton.titleLabel.font = LLRegularFont(17);
    self.rightButton.frame = CGRectMake(LLScreenWidth-100, self.navH-33, 80, 18);
    self.leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    self.rightButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
//    self.titlesArray = @[@"钢琴",@"钢琴1",@"钢琴2",@"钢琴3",@"钢琴4",@"钢琴5",@"钢琴6",@"钢琴7",@"钢琴9",@"钢琴10"];
//    self.titlesArray1 = @[@"555555 篇内容",@"555555 篇内容",@"555555 篇内容",@"555555 篇内容",@"555555 篇内容",@"555555 篇内容",@"555555 篇内容",@"555555 篇内容",@"555555 篇内容",@"555555 篇内容"];
    if (self.newsType == 100) {
        [self getNianjiData:@"0"];
    }else if(self.newsType == 200){
        [self getXingQuData:1];
    }else{
        [self getXingQuData:2];
    }

    [self searchView];
    
    // Do any additional setup after loading the view.
}

- (void)configTableView {
    self.needRefresh = NO;
    [super configTableView];
    self.tableView.mj_h = LLScreenHeight-self.navH-100;
    self.tableView.top = self.navH+100;
    self.tableView.backgroundColor = LLWhiteColor;
    [self.tableView registerCellByClassName:@"LLPublish2TableViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}

#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titlesArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 54;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0000001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.0000001;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LLPublish2TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LLPublish2TableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
   
    NSDictionary *dic = self.titlesArray[indexPath.row];
    if (self.newsType == 100) {
        cell.titleLabel.text = dic[@"name"];
    }else{
        cell.titleLabel.text = dic[@"interest_name"];
    }
    
//    cell.subtitleLabel.text = self.titlesArray1[indexPath.row];
    CGFloat wid = [CommonTools sizeOfText:cell.titleLabel.text fontSize:15 width:MAXFLOAT height:15].width;
    cell.titleLabel.frame = CGRectMake(24, 0, wid, 54);
    cell.subtitleLabel.frame = CGRectMake(29+wid, 0, LLScreenWidth-53-wid, 54);
        NSInteger oldRow = [_lastIndex row];
         NSInteger row = [indexPath row];

    if (row == oldRow && _lastIndex!=nil) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        
    }else{
            cell.accessoryType = UITableViewCellAccessoryNone;
            
        }

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ((self.isSecond) && self.newsType == 100) {
        NSDictionary *dic = self.titlesArray[indexPath.row];
        [self getNianjiData:dic[@"id"]];
    }else{
    NSInteger newRow = [indexPath row];
        _selectedIndex = newRow;
    NSInteger oldRow = (_lastIndex !=nil)?[_lastIndex row]:-1;
    if (newRow != oldRow) {
        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_lastIndex];
        oldCell.accessoryType = UITableViewCellAccessoryNone;
      _lastIndex = indexPath;
 }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}



#pragma mark - 右上角的动作点击事件
- (void)didButtonWithrightButton{
    [self fabuNews];
}

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

- (void)dealloc {
    self.navigationController.delegate = nil;
    [LLNotificationCenter removeObserver:self];
}


-(LLSearchView *)searchView{
    if (_searchView == nil) {
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, self.navH, LLScreenWidth, 100)];
        view1.backgroundColor = LLWhiteColor;
        [self.view addSubview:view1];
        
        UILabel *titleLabel = [LLInitBaseViewTool labelWithText:@"把内容投递到版块，让更多人看到" textColor:LLColorRGB(0) font:15 textAlignment:NSTextAlignmentLeft];
        titleLabel.frame = CGRectMake(15, 20, LLScreenWidth-30, 15);
        [view1 addSubview:titleLabel];
        
        self.searchView = [[LLSearchView alloc] initWithFrame:CGRectMake(15, 58, LLScreenWidth-30, 30)];
        self.searchView.backgroundColor = LLColorRGB(238);
        self.searchView.layer.masksToBounds = YES;
        self.searchView.layer.cornerRadius = 15;
        self.searchView.searchTxt.delegate = self;
        self.searchView.searchTxt.text = @"搜索板块";
        self.searchView.iamgebutton.hidden = YES;
        [self.searchView.searchTxt addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
        [view1 addSubview:self.searchView];
    }
    return _searchView;
}

- (void)textFieldChange:(UITextField *)textField {
    if (textField.markedTextRange == nil) {
        
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.searchView.searchTxt resignFirstResponder];
    if (textField.text.length !=0) {
        
        
    }
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    self.searchView.searchTxt.text = @"";
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

//获取年级
-(void)getNianjiData:(NSString *)type_id{
//    LLUserInfo *user = [LLUserInfo sharedUser];
    NSDictionary *dic= @{@"edu_id":@"27",@"type_id":type_id};
 
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
        self.titlesArray = result[@"data"];
        self.isSecond = !self.isSecond;
        [self.tableView reloadData];
        
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
        
    }];
}

//获取兴趣
-(void)getXingQuData:(NSInteger)type{
//    LLUserInfo *user = [LLUserInfo sharedUser];
    NSDictionary *dic = @{@"edu_id":@"10"};
    
    
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
         NSArray *dataArray = result[@"data"];
        NSMutableArray *array = [NSMutableArray array];
        for (NSDictionary *dic in dataArray) {
            if ([dic[@"type"] integerValue] == type) {
                [array addObject:dic];
            }
        }
        self.titlesArray = array.copy;
            [self.tableView reloadData];
       
        
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
        
    }];
}


//发布s新闻
-(void)fabuNews{
    LLUserInfo *user = [LLUserInfo sharedUser];
    NSInteger news_type ;
    if (self.newsType == 100) {
        news_type = 1;
    }else{
        news_type = 0;
    }
    NSDictionary *fenleiDic = self.titlesArray[_selectedIndex];
    NSInteger range ;
    NSString *interest_id;
    NSString *new_age;
    NSString *new_age_two;
    if (self.newsType == 100) {
       
        new_age = fenleiDic[@"pid"];
        new_age_two = fenleiDic[@"id"];
    }else{
      
       interest_id  = fenleiDic[@"id"];
    }
    if (self.isVideo) {
         range = 1;
    }else{
          range = 2;
    }
   
  
    NSMutableDictionary *dic =[NSMutableDictionary dictionaryWithDictionary: @{@"edu_id":@"11",@"user_id":user.user_id,@"user_random":user.user_random,@"range":@(range),@"new_type":@(news_type),@"new_title":self.name}];
    if (self.newsType == 100) {
        [dic setObject:new_age forKey:@"new_age"];
         [dic setObject:new_age_two forKey:@"new_age_two"];
    }else {
         [dic setObject:interest_id forKey:@"interest_id"];
    }
    if (news_type == 1) {//视频
        
    }else{//图文
        if (self.conent.length != 0) {
            [dic setObject:self.conent forKey:@"article"];
        }
        if (self.imageArr.count != 0) {
             [dic setObject:self.imageArr forKey:@"img_url"];
        }
        
    }
    
    
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
      
        if ([result[@"code"] integerValue] == 0) {
            NSLog(@"处理");
              [self showSVProgressHUDInfoWithsStatus:@"发布成功"];
            AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];//单例初始化方式
//            self.window.rootViewController = self.tabBarController;
//                app.tabbarVC.selectedIndex = 0;
//               [self.navigationController popToRootViewControllerAnimated:YES];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        else {

            [SVProgressHUD showErrorWithStatus:result[@"msg"]];
        }

    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
        
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
